from requests import post, Response

class GasInfo(object):
    def __init__(this):
        this._GasInfo__Referer = 'https://www.greatgas.cn/wechat/arrivePrice/wechatLNGArrivePrice'
        this._GasInfo__PostURL = '%sByDate' % this._GasInfo__Referer
        this._GasInfo__PostData = '{"arrivePriceMap":{"createDate":"%s"}}'
        this._GasInfo__Headers = {'Accept': 'application/json, text/javascript, */*; q=0.01', 
                                  'Accept-Encoding': 'gzip, deflate, br', 
                                  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 
                                  'Referer': this._GasInfo__Referer, 
                                  'User-Agent': None}

    def QueryByDate(this, Date, UserAgent):
        """
        ############################################
        [功能描述] 根据日期查询燃气到站价格.
        [参数列表] 1. Date      日期 (如: 20190402);
                   2. UserAgent 用户代理.
        [返回类型] dict.
        ############################################
        """

        responseData = Response()

        if this._GasInfo__Headers['User-Agent'] != UserAgent:
            this._GasInfo__Headers['User-Agent'] = UserAgent
        try:
            responseData = post(this._GasInfo__PostURL, 
                                data = this._GasInfo__PostData % Date, 
                                headers = this._GasInfo__Headers)
            return responseData.json()
        except Exception as e:
            print(e.args)